# -*- coding: utf-8 -*-
import tkinter as tk
import file_operate
import json
import os
import buildtable
import pathcfg


list = ["数据表读取文件夹目录", "生成数据表json文件目录", "生成数据表cs文件目录", "数据表配置模板文件目录", "数据表类模板文件目录"]
name_list = ["rxlsx_dir_path", "wjson_dir_path", "wcs_dir_path", "data_table_cfg_path", "data_table_class_path"]

# Create the main window
root = tk.Tk()
root.title("生成数据表")
root.geometry("400x600")
root.geometry("+300+200")
root.resizable(width=False, height=False)

current_dir = os.path.expanduser("~\AppData\Roaming\DMGame")
file_operate.check_dir(current_dir)
cur_cfg = file_operate.read_file(current_dir, "userdata", ".json")
if not cur_cfg:
    cur_cfg = "{}"
path_cfg_json = json.loads(cur_cfg)
for name in name_list:
    if name not in path_cfg_json:
        path_cfg_json[name] = ""
    pathcfg.__dict__[name] = path_cfg_json[name]

def path_cfg(event, name, path):
    content = file_operate.read_file(current_dir, "userdata", ".json")
    if not content:
        content = "{}"
    jsonstr = json.loads(content)
    jsonstr[name] = path
    file_operate.write_file(json.dumps(jsonstr), current_dir, "userdata", ".json")
    # json = {}
    # json["name"] = name.
    # json["path"] = path
    # file_operate.write_file(json, current_dir, "userdata", ".txt")

def create_node(i):
    label = tk.Label(root, text=list[i])
    label.grid(row=i, column=0, pady=10)
    input = tk.Entry(root)
    input.bind("<KeyRelease>", lambda event: path_cfg(event, name_list[i], input.get()))
    input.grid(row=i, column=1, pady=10, ipadx=50)
    input.insert(0, path_cfg_json[name_list[i]])

def create_data_table():
    file_operate.clear_all_files(pathcfg.wjson_dir_path)
    file_operate.clear_all_files(pathcfg.wcs_dir_path)
    buildtable.build_table(pathcfg.rxlsx_dir_path, '*.xlsx')

for i in range(5):
    create_node(i)

button = tk.Button(root, text="生成数据表", command=create_data_table)
button.grid(row=5, column=0, columnspan=2, pady=10)

root.mainloop()